#include "/usr/include/python2.7/Python.h"
#include <stdio.h>

int main(int arg,char **argv){
    PyObject *modelname,*model,*dict,*func,*args;
    char *name="os";//模块名

    //初始化Python
    Py_Initialize();
    if (!Py_IsInitialized()){
        printf("初始化失败\n");
        return -1;
    }

    //直接运行Python语句
    PyRun_SimpleString("print '初始化成功'");

    //导入Python模块
    modelname=PyString_FromString(name);
    model=PyImport_Import(modelname);

    if (model){
        printf("Load model ok\n");
    }
    else{
        printf("Model %s not found!\n",name);
        return -1;
    }

    dict=PyModule_GetDict(model);

    if(!dict){
        printf("获取字典失败\n");
        return -1;
    }
    else{
        printf("获取字典成功\n");   
    }

    //从模块中找到“System”函数
    func=PyDict_GetItemString(dict,"system");

    if(!func || !PyCallable_Check(func)){
        printf("函数无效\n");
        return -1;   
    }

    args=PyTuple_New(1);

    PyTuple_SetItem(args,0,Py_BuildValue("s","ls"));//l＝long，s＝string...

    //调用函数
    PyObject_CallObject(func,args);

    Py_DECREF(modelname);
    Py_DECREF(model);
    Py_DECREF(func);
    Py_DECREF(args);
    Py_DECREF(dict);
    //垃圾回收
    Py_Finalize();
    return 0;
}
